<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Activation;

/**
 * Base implementation of ActivatorInterface
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class BaseActivator implements ActivatorInterface
{
	/**
	 * Manages the activation process from start to end for a given session
	 *
	 * @param SessionInterface $session
	 * @return void
	 */
	public function activate(SessionInterface $session)
	{
		$contributors = $session->getContributorRegistry();
		while(true) {
			$currentContributors = $contributors->getContributors($session->getStage());
			foreach($currentContributors as $contributor) {
				$contributor->onActivation($session);
			}
			
			switch($session->getStage()) {
				case SessionStage::PreCollection:
					$session->setStage(SessionStage::PostCollection());
					break;
				case SessionStage::PostCollection:
					$session->setStage(SessionStage::Factory());
					break;
				case SessionStage::Factory:
					$session->setStage(SessionStage::Construction());
					break;
				case SessionStage::Construction:
					$session->setStage(SessionStage::PostConstruction());
					break;
				case SessionStage::PostConstruction:
					$session->setStage(SessionStage::Injection());
					break;
				case SessionStage::Injection:
					$session->setStage(SessionStage::PostInjection());
					break;
				case SessionStage::PostInjection:
					$session->setStage(SessionStage::Lifecycle());
					break;
				case SessionStage::Lifecycle:
					$session->setStage(SessionStage::PostActivation());
					break;
				case SessionStage::PostActivation:
					return;
			}
		}
	}
}